package com.czk.client;

import com.czk.pojo.Invocation;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/20 10:52
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter implements Callable {
    private Invocation invocation;
    private ChannelHandlerContext channelHandlerContext;
    private Object result;
    @Override
    public  synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        this.result=msg;
        notify();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接到服务器了");
        this.channelHandlerContext=ctx;
    }

    @Override
    public synchronized Object call() throws Exception {
        System.out.println("进入写方法了");
        //  写到服务器
        channelHandlerContext.writeAndFlush(invocation);
        wait();
        return result;
    }

    public Invocation getInvocation() {
        return invocation;
    }

    public void setInvocation(Invocation invocation) {
        this.invocation = invocation;
    }
}
